﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CrazyCancer.Framework.ShortUrl.Interfaces;
using System.Net;
using CrazyCancer.Framework.Communication.Interfaces;
using CrazyCancer.Framework.Communication;
using CrazyCancer.Framework.Communication.Enums;
using System.IO;
using System.Web;

namespace CrazyCancer.Framework.ShortUrl
{
    public class TinyUrl : IUrlShortener
    {
        #region IUrlShortener Members

        public string ShortenUrl(string longUrl, string apiUrl, ICommunicator communicator)
        {
            string longUrlEncoded = HttpUtility.UrlEncode(longUrl);

            StringBuilder urlBuilder = new StringBuilder();
            urlBuilder.Append(apiUrl);
            urlBuilder.Append(longUrlEncoded);

            WebResponse webResponse = communicator.SendRequest(urlBuilder.ToString(), RequestMethod.GET);

            StreamReader responseStream = new StreamReader(webResponse.GetResponseStream()); 
            String response = responseStream.ReadToEnd(); 
            responseStream.Close();

            return response;
        }

        public string ShortenUrl(string longUrl, string apiUrl, ICommunicator communicator, string userName, string password)
        {
            return ShortenUrl(longUrl, apiUrl, communicator);
        }

        #endregion
    }
}
